package com.example.week2_try2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyTask implements Runnable{
    private static final String TAG = "MyTask";
    private Handler handler;
    private ArrayList<HashMap<String,String>> listItems;
    //不调用该方法会显示空指针异常
    public void setHandler(Handler handler){
        this.handler = handler;
    }

    @Override
    public void run() {
        List<String> list2 = new ArrayList<String>();
        listItems = new ArrayList<HashMap<String,String>>();
        Bundle bundle=new Bundle();
        try {
            Document doc = null;
            doc = Jsoup.connect("https://usd-cny.com/").get();
            Log.i(TAG, "run: "+doc.title());
            Elements tables=doc.getElementsByTag("table");
            Element firstTable=tables.first();//单位元素
            //通过行获取数据
            Elements trs=firstTable.getElementsByTag("tr");
            trs.remove(0);
            for(Element tr:trs){
                Log.i(TAG, "run: tr="+tr);
                //从行中获得td元素
                Elements tds=tr.getElementsByTag("td");
                //获取第一列
                Element td1=tds.get(0);
                Element td2=tds.get(4);
                Log.i(TAG, "run: td1="+td1.text()+"\n run: td2="+td2.text());
                if(!(td2.text().equals(""))){
                    HashMap<String,String> map = new HashMap<String,String>();//map一定要在循环里创建，在外面的话就是同一个map不停的放进list

                    float rate = 100f/Float.parseFloat(td2.text());//计算汇率
                    list2.add(td1.text()+"------>"+String.valueOf(rate));
                    map.put("ItemTitle","Rate: = "+td1.text());//标题文字
                    map.put("ItemDetail","detail: = "+String.valueOf(rate));//详情描述
                    listItems.add(map);
                }
                else{
                    HashMap<String,String> map = new HashMap<String,String>();//map一定要在循环里创建，在外面的话就是同一个map不停的放进list
                    map.put("ItemTitle","Rate: = "+td1.text());//标题文字
                    map.put("ItemDetail","detail: = "+td2.text());//详情描述
                    listItems.add(map);
                    list2.add(td1.text()+"------>"+td2.text());
                }

                //加过滤项
//                if(td1.text().equals("美元")){
//                    float dollar_rate = 100f/Float.parseFloat(td2.text());
//                    list2.add("美元"+String.valueOf(dollar_rate));
//                    bundle.putFloat("dollar_rate",dollar_rate);
//                }
//                else if(td1.text().equals("欧元")){
//                    float euro_rate = 100f/Float.parseFloat(td2.text());
//                    list2.add("欧元"+String.valueOf(euro_rate));
//
//                    bundle.putFloat("euro_rate",euro_rate);
//                }
//                else if(td1.text().equals("韩币")){
//                    float won_rate = 100f/Float.parseFloat(td2.text());
//                    list2.add("韩币"+String.valueOf(won_rate));
//
//                    bundle.putFloat("won_rate",won_rate);
//                }
            }
            Message msg=handler.obtainMessage(9,listItems);//括号内可写what

            handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
